package com.barbeque.mall.controller.wx;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.wx.topic.TopicDetail;
import com.barbeque.mall.bean.wx.topic.WxMarketTopic;
import com.barbeque.mall.bean.wx.topic.WxMarketTopicGoods;
import com.barbeque.mall.service.wx.WxTopicServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Tezel
 * @since 2022/07/18 16:49
 */
@RestController
@RequestMapping("wx/topic")
public class WxTopicController {

    @Autowired
    WxTopicServiceImpl wxTopicService;

    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info) {
        CommonData<WxMarketTopic> data = wxTopicService.selectTopicList(info);
        BaseRespVo<Object> objectBaseRespVo = new BaseRespVo<>();
        objectBaseRespVo.setErrmsg("成功");
        BaseRespVo baseRespVo = BaseRespVo.ok(data);
        return baseRespVo;
    }

    @RequestMapping("detail")
    public BaseRespVo detail(@RequestParam("id") Integer id) {

        TopicDetail topicDetail = new TopicDetail();
        WxMarketTopic topic = wxTopicService.selectTopicDetail(id);
        topicDetail.setTopic(topic);

        //遍历查到的topic里面的id,然后根据这个ID进行查找操作
        ArrayList<WxMarketTopicGoods> goodsArrayList = new ArrayList<>();
        Integer[] goodsId = topic.getGoods();
        for (Integer ids : goodsId) {
            if (ids != null){
                WxMarketTopicGoods topicGoodsList = wxTopicService.selectTopicGoodsList(ids);
                goodsArrayList.add(topicGoodsList);
            }
        }
        List<WxMarketTopicGoods> goodsList = goodsArrayList;
        topicDetail.setGoods(goodsList);

        BaseRespVo<TopicDetail> topicDetailBaseRespVo = new BaseRespVo<>();
        topicDetailBaseRespVo.setErrmsg("成功");
        return BaseRespVo.ok(topicDetail);
    }

    @RequestMapping("related")
    public BaseRespVo related(){
        CommonData<WxMarketTopic> data = wxTopicService.selectTopicRelated();
        BaseRespVo<Object> objectBaseRespVo = new BaseRespVo<>();
        objectBaseRespVo.setErrmsg("成功");
        BaseRespVo baseRespVo = BaseRespVo.ok(data);
        return baseRespVo;
    }
}
